﻿#include "mlabel.h"
#include <QPainter>
#include <QTimer>
#include <QFontMetrics>
#include <QTimerEvent>
#include <QDebug>

const QString strSpace("   ");

MLabel::MLabel(QWidget *parent) : QLabel(parent)
{
    setStyleSheet(QString("background-color: rgb(0, 0, 0);font: 12pt \"Arial\";color: rgb(255, 0, 0);"));

    timerId = -1;
    m_nSpeed = 3;

    m_bImgOrText = false;
    m_pImage = NULL;

    m_nEffect = Effect_Static;
    m_bLoop = false;

    m_nRoll_Left = 0;
    m_nRoll_Top = 0;

    m_nImgScale = 1;
    m_nActImgWidth = rect().width();
    m_nActImgHeight = rect().height();

    upateLabelRollingState();
}

void MLabel::setPlayEffect(enumPlay_Effect nEffect)
{
    QRect rc = rect();

    m_nEffect = nEffect;

    if(m_nEffect == Effect_Static)
    {
        if(timerId >= 0)
        {
            killTimer(timerId);
            timerId = -1;
            repaint();
        }
    }
    else if(m_nEffect == Effect_MoveLeft)
    {
        m_nRoll_Left = rc.width();

        if(m_bImgOrText)
        {
            //图片，以最大高度重新装载
            int nImgWidth  = m_pImage->width();
            int nImgHeight = m_pImage->height();

            int nWidth  = rect().width();
            int nHeight = rect().height();

            //显示比例图像
            int m_nImgScale = (1.0 * nImgHeight / nHeight) * 100;

            m_nActImgWidth = (int)(100.0 * m_pImage->width() / m_nImgScale);
            m_nActImgHeight = (int)(100.0 * m_pImage->height() / m_nImgScale);
        }

        if(timerId <= 0)
        {
            timerId = startTimer(g_nTimerInterval[m_nSpeed]);
        }
    }
    else if(m_nEffect == Effect_ScrollUp)
    {
        if(m_bImgOrText)
        {
            m_nRoll_Top = rc.bottom();

            if(m_bImgOrText)
            {
                int nImgWidth  = m_pImage->width();
                int nImgHeight = m_pImage->height();

                int nWidth  = rect().width();
                int nHeight = rect().height();

                //显示比例图像
                int nScale_Width = (1.0 * nImgWidth / nWidth) * 100;
                int nScale_Height = (1.0 * nImgHeight / nHeight) * 100;

                if(nScale_Width > nScale_Height) //宽度比例大，按高度比例来
                {
                    m_nImgScale = nScale_Width;
                }
                else //
                {
                    m_nImgScale = nScale_Height;
                }

                m_nActImgWidth = (int)(100.0 * m_pImage->width() / m_nImgScale);
                m_nActImgHeight = (int)(100.0 * m_pImage->height() / m_nImgScale);

                if(m_nActImgWidth > rect().width())
                    m_nActImgWidth = rect().width();

                if(m_nActImgHeight > rect().height())
                    m_nActImgHeight = rect().height();
            }

            if(timerId <= 0)
            {
                timerId = startTimer(g_nTimerInterval[m_nSpeed]);
            }
        }
        else
        {
            setPlayEffect(Effect_MoveLeft);
        }
    }
}

void MLabel::setPlaySpeed(int nSpeed)
{
    m_nSpeed = nSpeed;

    if(timerId > 0 && isVisible())
    {
        killTimer(timerId);

        timerId = startTimer(g_nTimerInterval[m_nSpeed]);
    }
}

int MLabel::getPlayTimes()
{
    QRect rc = rect();

    int nMiSeconds = 0;

    if(m_bImgOrText)
    {
        nMiSeconds = (m_nActImgHeight + rc.height()) / m_nSpeed;
    }
    else
    {
        nMiSeconds = (m_nActImgWidth + rc.width()) / m_nSpeed;
    }

    return nMiSeconds;
}

bool MLabel::createAvi()
{
//    Mat image;

//    char filename[75]; //定义数组，其中数组大小大于等于图片的个数
//    int isColor = 1;   //如果为0 ，可输出灰度图像
//    //int frameWidth = image.cols;   //单帧图片的宽
//    //int frameHeight = image.rows;  //单帧图片的高

//    QRect rc= rect();

//    int nMs = getPlayTimes();

//    QImage* pImage = new QImage(rc.width(), rc.height(), QImage::Format_RGB32);
//    QPainter painterImg(pImage);

//    if(m_bImgOrText)
//    {
//        int fps = 25;      //设置输出视频的帧率
//        VideoWriter writer("d:/video_out.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, Size(rc.width(), rc.height()),isColor);

//        int nFileIndex = 1;
//        for(int nPosY = rc.height(); nPosY > -m_nActImgHeight; )
//        {
//            QString strFiles = QString("%1%2.bmp").arg(AppConfig::strMediaFilePath).arg(nFileIndex);
//            strFiles = QString("%10001.bmp").arg(AppConfig::strMediaFilePath);
//            nFileIndex ++;
//            sprintf(filename,"%s", strFiles.toLatin1().data());//第二个参数是指定图片路径和图片名的一般式（image%d）

//            pImage->fill(QColor(0, 0, 0, 0));

////            QRectF target(0, nPosY, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
//            QRectF target((rc.width() - m_nActImgWidth) / 2, nPosY, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
//            QRectF source(0, 0, m_pImage->width(), m_pImage->height()); //建立源矩形，用来框定源图像文件中要显示的部分
//            painterImg.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的

//            pImage->save(filename);

//            image=imread(filename);//导入图片
//            if (image.empty())
//            {
//                break;
//            }
//            waitKey(0);
//            //cout << image.channels() << endl; //为了解决问题而打印查看图片的通道
//            //cout << image.size() << endl;     //为了解决问题而打印图片的尺寸
//            writer.write(image); //
//            nPosY -= 1;
//        }
//    }
//    else
//    {
//        int fps = 75;      //设置输出视频的帧率
//        VideoWriter writer("d:/video_out.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, Size(rc.width(), rc.height()),isColor);

//        painterImg.setFont(m_ft);
//        painterImg.setPen(QPen(Qt::white));
//        int nFileIndex = 1;
//        for(int nPosX = rc.width(); nPosX > -m_nActImgWidth; )
//        {
//            QString strFiles = QString("%1%2.bmp").arg(AppConfig::strMediaFilePath).arg(nFileIndex);
//            strFiles = QString("%10001.bmp").arg(AppConfig::strMediaFilePath);
//            nFileIndex ++;
//            sprintf(filename,"%s", strFiles.toLatin1().data());//第二个参数是指定图片路径和图片名的一般式（image%d）

//            pImage->fill(QColor(0, 0, 0, 0));
//            painterImg.drawText(nPosX, m_nActImgHeight - 4, this->text());
//            pImage->save(filename);

//            image=imread(filename);//导入图片
//            if (image.empty())
//            {
//                break;
//            }
//            waitKey(0);
//            //cout << image.channels() << endl; //为了解决问题而打印查看图片的通道
//            //cout << image.size() << endl;     //为了解决问题而打印图片的尺寸
//            writer.write(image); //
//            nPosX -= 2;
//        }
//    }

    return true;
}

void MLabel::setText(const QString & txt)
{
    QLabel::setText(txt);

    m_bImgOrText = false;
    upateLabelRollingState();
}

void MLabel::setImage(const QString &strImgFile)
{
    if(m_pImage)
    {
        delete m_pImage;
        m_pImage = NULL;
    }

    m_bImgOrText = true;
    m_pImage = new QImage(strImgFile);

    int nImgWidth  = m_pImage->width();
    int nImgHeight = m_pImage->height();

    int nWidth  = rect().width();
    int nHeight = rect().height();

    //显示比例图像
    int nScale_Width = (1.0 * nImgWidth / nWidth) * 100;
    int nScale_Height = (1.0 * nImgHeight / nHeight) * 100;

    if(nScale_Width > nScale_Height) //宽度比例大，按高度比例来
    {
        m_nImgScale = nScale_Width;
    }
    else //
    {
        m_nImgScale = nScale_Height;
    }

    m_nActImgWidth = (int)(100.0 * m_pImage->width() / m_nImgScale);
    m_nActImgHeight = (int)(100.0 * m_pImage->height() / m_nImgScale);

    if(m_nActImgWidth > rect().width())
        m_nActImgWidth = rect().width();

    if(m_nActImgHeight > rect().height())
        m_nActImgHeight = rect().height();

    repaint();
}

void MLabel::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    QRect rc = rect();

    if(m_bImgOrText)
    {
        if(m_nEffect == Effect_Static)
        {
            if(m_nActImgWidth < rc.width())
            {
                //居中
                QRectF target((rc.width() - m_nActImgWidth) / 2, 0, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
                QRectF source(0, 0, m_pImage->width(), m_pImage->height()); //建立源矩形，用来框定源图像文件中要显示的部分
                painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
            }
            else
            {
                //左对齐
                QRectF target(0, (rc.height() - m_nActImgHeight) / 2, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
                QRectF source(0, 0, m_pImage->width(), m_pImage->height()); //建立源矩形，用来框定源图像文件中要显示的部分
                painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
            }
        }
        else if(m_nEffect == Effect_MoveLeft)
        {
            QBrush brush;
            brush.setColor(Qt::black);//设置颜色
            brush.setStyle(Qt::SolidPattern);
            painter.setBrush(brush);

            painter.fillRect(rc, brush);//绘制矩形

            QRectF target(m_nRoll_Left, 0, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
            QRectF source(0, 0, m_pImage->width(), m_pImage->height()); //建立源矩形，用来框定源图像文件中要显示的部分
            painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
        }
        else if(m_nEffect == Effect_ScrollUp)
        {
            QBrush brush;
            brush.setColor(Qt::black);//设置颜色
            brush.setStyle(Qt::SolidPattern);
            painter.setBrush(brush);

            painter.fillRect(rc, brush);//绘制矩形

//            QRectF target(0, m_nRoll_Top, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
//            QRectF source(0, 0, m_pImage->width(), m_pImage->height()); //建立源矩形，用来框定源图像文件中要显示的部分
//            painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的

            if(m_nActImgWidth < rc.width())
            {
                //居中
                QRectF target((rc.width() - m_nActImgWidth) / 2, m_nRoll_Top, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
                QRectF source(0, 0, m_pImage->width(), m_pImage->height()); //建立源矩形，用来框定源图像文件中要显示的部分
                painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
            }
            else
            {
                //左对齐
                QRectF target(0, (rc.height() - m_nActImgHeight) / 2, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
                QRectF source(0, 0, m_pImage->width(), m_pImage->height()); //建立源矩形，用来框定源图像文件中要显示的部分
                painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
            }
        }
    }
    else
    {
        if(m_nEffect == Effect_Static)
        {
            if(m_nActImgWidth < rc.width())
            {
                //居中
                QRectF target((rc.width() - m_nActImgWidth) / 2, 0, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
                QRectF source(0, 0, m_nActImgWidth, m_nActImgHeight); //建立源矩形，用来框定源图像文件中要显示的部分
                painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
            }
            else
            {
                //左对齐
                QRectF target(0, 0, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
                QRectF source(0, 0, m_nActImgWidth, m_nActImgHeight); //建立源矩形，用来框定源图像文件中要显示的部分
                painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
            }
        }
        else
        {
            QBrush brush;
            brush.setColor(Qt::black);//设置颜色
            brush.setStyle(Qt::SolidPattern);
            painter.setBrush(brush);

            painter.fillRect(rc, brush);//绘制矩形

            QRectF target(m_nRoll_Left, 0, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
            QRectF source(0, 0, m_nActImgWidth, m_nActImgHeight); //建立源矩形，用来框定源图像文件中要显示的部分
            painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
        }
    }
}

void MLabel::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == timerId && isVisible())
    {
        QRect rc = rect();

        if(m_nEffect == Effect_MoveLeft)
        {
            //  每次左移1个像素
            m_nRoll_Left -= 1;

            if(m_bImgOrText)
            {
                if(m_nRoll_Left < -m_nActImgWidth)
                    m_nRoll_Left = rect().width();
            }
            else
            {
                //  判断是否已经完成一遍循环，完成则恢复起始位置，重新开始循环
                if(m_nRoll_Left < -m_nActImgWidth)
                {
                    if(m_bLoop)
                    {
                        m_nRoll_Left = rc.width();
                    }
                    else
                    {
                        setPlayEffect(Effect_Static);
                    }
                }
            }
        }
        else if(m_nEffect == Effect_ScrollUp)
        {
            //  每次上移1个像素
            m_nRoll_Top -= 1;

            if(m_bImgOrText)
            {
                if(m_nRoll_Top < -m_nActImgHeight)
                {
                    if(m_bLoop)
                    {
                        m_nRoll_Top = rc.height();
                    }
                    else
                    {
                        setPlayEffect(Effect_Static);
                    }
                }
                else
                {
                    qDebug() << "Effect_ScrollUp: " <<m_nRoll_Top;
                }
            }
            else
            {
                //  判断是否已经完成一遍循环，完成则恢复起始位置，重新开始循环
                if(m_nRoll_Top < -m_nActImgHeight)
                {
                    if(m_bLoop)
                    {
                        m_nRoll_Top = rc.height();
                    }
                    else
                    {
                        setPlayEffect(Effect_Static);
                    }
                }
            }
        }

        repaint();
    }

    QLabel::timerEvent(e);
}

void MLabel::resizeEvent(QResizeEvent *e)
{
    QLabel::resizeEvent(e);

    upateLabelRollingState();
}

void MLabel::upateLabelRollingState()
{
    if(m_bImgOrText)
        return;

    QRect rc = rect();
    fontSize = rc.height();

    m_ft = font();
    m_ft.setPointSize(fontSize - 6);

    QFontMetrics fm(m_ft);

    QString strText = text();

    m_nActImgWidth  = fm.width(strText);
    m_nActImgHeight = rc.height();

    if(m_pImage)
    {
        delete m_pImage;
        m_pImage = NULL;
    }

    m_pImage = new QImage(m_nActImgWidth, m_nActImgHeight, QImage::Format_RGB32);

    QPainter painterImg(m_pImage);
    m_pImage->fill(QColor(0, 0, 0, 0));

    painterImg.setFont(m_ft);
    painterImg.setPen(QPen(Qt::white));

    painterImg.drawText(0, m_nActImgHeight - 4, strText);
}
